﻿using Entity;
using IRealize;
using Microsoft.Data.SqlClient;
using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace Realize
{
    public class GM :IGM
    {
        public async Task<List<Group_Menu>> Server(string sqlserver)
        {
            Task<List<Group_Menu>> task = Task<List<Group_Menu>>.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(sqlserver))
                {
                    string sql = $"select * from group_menu";
                    return con.Query<Group_Menu>(sql).ToList();
                }
            });
            return await task;
        }
        public async Task<int> Delete(int id, string sqlserver)
        {
            using (SqlConnection con = new SqlConnection(sqlserver))
            {
                string sql = $"delete from group_menu where group_id = ${id}";
                return await con.ExecuteAsync(sql);

            }
        }
        public async Task<List<Group_Menu>> Server2(int id, string sqlserver)
        {
            Task<List<Group_Menu>> task = Task<List<Group_Menu>>.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(sqlserver))
                {

                    string sql = $"select * from group_menu where group_id = {id}";
                    return con.Query<Group_Menu>(sql).ToList();
                }
            });
            return await task;
        }
        public async Task<int> Update(int id, string group_name, string sqlserver)
        {
            using (SqlConnection con = new SqlConnection(sqlserver))
            {
                string sql = $"update group_menu set group_name = '{group_name}' WHERE group_id = {id}";
                return await con.ExecuteAsync(sql);

            }
        }

        public async Task<int> Insert(string group_name, string sqlserver)
        {
            using (SqlConnection con = new SqlConnection(sqlserver))
            {
                string sql = $@"insert into group_menu(group_name)
                 values('{group_name}')";
                return await con.ExecuteAsync(sql);

            }
        }
    }
}
